<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <input type="text" />
    <button>提交</button>
    <script>
      // 防抖函数
      let button = document.querySelector('button');
      let input = document.querySelector('input');
      function submit() {
        console.log(input.value);
      }
      button.addEventListener('click', throttled1(submit, 2000));
      // function throttled(fn, delay) {
      //   let timer = null;
      //   return function () {
      //     if (!timer) {
      //       timer = setTimeout(() => {
      //         fn.apply(this, arguments);
      //         timer = null;
      //       }, delay);
      //     }
      //   };
      // }
      function throttled1(fn, delay) {
        let oldtime = Date.now();
        return function () {
          let newtime = Date.now();
          if (newtime - oldtime >= delay) {
            fn.apply(null, ...arguments);
            oldtime = Date.now();
          }
        };
      }
    </script>
  </body>
</html>
